﻿using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using DotNetExtensions.AspNet;
using DotNetExtensions.AspNet.DevExpress.MVVM.ViewModels;
using Habi.Solver.Common;
using Habi.Solver.Common.ViewModel;
using Habi.Solver.Helpers;
using Habi.Solver.Models;
using Habi.Solver.Models.Resources;
using Habi.Solver.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Habi.Solver.ViewModels.Resources
{
    public class UploadResourceViewModel : BaseViewModel<UploadResourceModel>
    {
        public virtual IList<ResourceTaskModel> Tasks { get; set; }
        public virtual IList<FileModel> Files { get; set; }
        public virtual IList<FileModel> Selected { get; set; }

        public virtual string Description { get; set; }
        public virtual string AboutUrl { get; set; }
        public virtual string Category { get; set; }
        public virtual IEnumerable<string> Categorys { get; set; }


        public UploadResourceViewModel()
        {
            Categorys = new List<string>
            {
                "Application",
                "Video",
                "Music"
            };
            Tasks = new List<ResourceTaskModel>();
            Files = new List<FileModel>();
        }

        public void Load()
        {

        }

        public void Add(ResourceTaskModel task)
        {
            if (Tasks.Any(t => t.Url == task.Url)) return;

            Tasks.Add(task);
            int index = 1;
            Files = Tasks.Select(c => new FileModel { Index = index++, Name = c.Name, Size = c.SizeText, Id = c.Id }).ToList();
        }

        public override bool Validate()
        {
            bool valid = ValidationService.Validate();
            if (!valid) return false;

            if (!Files.Any())
            {
                MessageService.Warning("Please select a file "); return false;
            }


            return true;
        }

        public override void Save()
        {
            if (!Validate()) return;

            TaskContainer.Default.Add(Tasks, true);

            List<ResourceModel> data = Tasks.Select(t => new ResourceModel
            {
                AboutUrl = AboutUrl,
                AutoDownload = true,
                CreateTime = DateTime.Now,
                Description = Description,
                Downloads = 0,
                Id = t.Id,
                Name = t.Name,
                Type = ResourceType.Application,
                Extension = t.Extension,
                Size = t.Size
            }).ToList();

            ResourceContainer.Default.Add(data);
            Close();
        }

        public void Remove(IList<FileModel> models)
        {
            if (models != null && models.Any())
            {
                var dialog = MessageService.Confirm("Are you sure ?");
                if (dialog == System.Windows.Forms.DialogResult.Yes)
                {
                    var removed = (from m in models join t in Tasks on m.Id equals t.Id select t).ToList();
                    Tasks = Tasks.Except(removed).ToList();
                    int index = 1;
                    Files = Tasks.Select(c => new FileModel { Index = index++, Name = c.Name, Size = c.SizeText, Id = c.Id }).ToList();
                }
            }
        }

        public void Open()
        {
            //var files = OperationService.OpenFileDialog("", true);
            //foreach (var file in files)
            //{
            //    var fileInfo = new FileInfo(file);

            //    string ext = fileInfo.Extension;
            //    if (string.IsNullOrEmpty(ext)) ext = ".unkown";

            //    var item = new ResourceTaskModel
            //    {
            //        Id = Snowflake.Default.NextShortId(),
            //        Icon = ext.Trim().ToUpper(),
            //        Url = file,
            //        Name = fileInfo.Name,
            //        Progress = 0,
            //        Size = fileInfo.Length,
            //        Status = (int)ResourceTaskStatus.Ready,
            //        Type = (int)TaskType.Upload,
            //        Extension = ext
            //    };

            //    Add(item);
            //}
        }
    }
}
